package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.HispitalDao;
import com.example.child_protection.dto.info.HispitalDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.HispitalRequestDTO;
import com.example.child_protection.dto.request.find.SystemMenuRequestDTO;
import com.example.child_protection.dto.response.HispitalListResponseDTO;
import com.example.child_protection.dto.response.HispitalResponseDTO;
import com.example.child_protection.dto.response.SystemMenuListResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.HispitalModel;
import com.example.child_protection.service.HispitalQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 医院查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class HispitalQueryServiceImpl extends MyBaseService implements HispitalQueryService {

    @Autowired
    public HispitalDao hispitalDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public HispitalResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.HISPITAL_FIND_ID, requestDTO, HispitalResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, HispitalResponseDTO>() {
            @Override
            protected HispitalResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                HispitalResponseDTO responseDTO = new HispitalResponseDTO();
                HispitalModel hispitalModel = hispitalDao.findId(requestDTO.getId());
                HispitalDTO hispital = new HispitalDTO();
                BeanUtils.copyProperties(hispitalModel, hispital);
                responseDTO.setInfo(hispital);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public HispitalListResponseDTO findList(HispitalRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.HISPITAL_FIND_LIST, requestDTO, HispitalListResponseDTO.class, new MyServiceExecutor<HispitalRequestDTO, HispitalListResponseDTO>() {
            @Override
            protected HispitalListResponseDTO myprocess(HispitalRequestDTO requestDTO) throws Exception {
                HispitalListResponseDTO responseDTO = new HispitalListResponseDTO();
                HispitalModel req = new HispitalModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<HispitalModel> lists = hispitalDao.findPage(req);
                List<HispitalDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), HispitalDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
